﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MCPlatform.Communicate.TcpIp
{
    public class Message
    {
        private TcpClient _tcpClient;

        private Encoding _encoder = Encoding.Default;

        private byte _writeLineDelimiter;

        private bool _autoTrim = false;

        public byte[] Data { get; private set; }

        public string MessageString
        {
            get
            {
                if (_autoTrim)
                {
                    return _encoder.GetString(Data).Trim();
                }

                return _encoder.GetString(Data);
            }
        }

        public TcpClient TcpClient => _tcpClient;

        internal Message(byte[] data, TcpClient tcpClient, Encoding stringEncoder, byte lineDelimiter)
        {
            Data = data;
            _tcpClient = tcpClient;
            _encoder = stringEncoder;
            _writeLineDelimiter = lineDelimiter;
        }

        internal Message(byte[] data, TcpClient tcpClient, Encoding stringEncoder, byte lineDelimiter, bool autoTrim)
        {
            Data = data;
            _tcpClient = tcpClient;
            _encoder = stringEncoder;
            _writeLineDelimiter = lineDelimiter;
            _autoTrim = autoTrim;
        }

        public void Reply(byte[] data)
        {
            _tcpClient.GetStream().Write(data, 0, data.Length);
        }

        public void Reply(string data)
        {
            if (!string.IsNullOrEmpty(data))
            {
                Reply(_encoder.GetBytes(data));
            }
        }

        public void ReplyLine(string data)
        {
            if (!string.IsNullOrEmpty(data))
            {
                if ((uint)data.LastOrDefault() != _writeLineDelimiter)
                {
                    Reply(data + _encoder.GetString(new byte[1] { _writeLineDelimiter }));
                }
                else
                {
                    Reply(data);
                }
            }
        }
    }
}

